<template>
  <el-dialog
    :append-to-body="true"
    @close="closeDialog"
    :visible.sync="dialog"
    :title="is_edit?'编辑岗位':'新增岗位'"
    width="500px"
  >
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="岗位名称" prop="name">
        <el-input placeholder="输入岗位名称" v-model="form.name" style="width: 320px;" maxlength="20" />
      </el-form-item>
      <el-form-item label="排序" prop="sort" v-if="0">
        <el-input-number
          v-model.number="form.sort"
          :min="0"
          :max="99"
          controls-position="right"
          style="width: 320px;text-align:left;"
        />
      </el-form-item>
      <!--<el-form-item label="状态" prop="pid">
        <el-radio-group v-model="form.enabled">
          <el-radio v-for="item in statusModul" :key="item.id" :label="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>-->
      <el-form-item label="所属部门"  prop="dept.id">
        <el-select v-model="form.dept.id" :disabled="level==2" placeholder="选择所属部门" style="width:320px;">
          <el-option
            v-for="item in depts"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
        <!-- <treeselect :disabled="level==2" v-model="form.dept.id" :options="depts" style="width: 320px;" placeholder="选择上级部门" /> -->
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="loading" size="mini" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { _jobCreate, _jobUpdate, _deptList } from "@/http/ajax";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: { Treeselect },
  name: "edit",
  props: {
    is_edit: {
      type: Boolean,
      required: true
    },
    cur_this: {
      type: Object,
      required: null
    }
  },
  data() {
    return {
      depts: [],
      statusModul: [
        { id: 1, label: "启用", value: true },
        { id: 2, label: "禁用", value: false }
      ],
      dialog: false,
      loading: false,
      form: {
        name: "",
        sort: 1,
        enabled: true,
        dept: {
          id: ''
        }
      },
      rules: {
        name: [{ required: true, message: "请输入岗位名称", trigger: "blur" }],
        'dept.id': [{ required: true, message: "请选择所属部门", trigger: "change" }]
      },
      level:sessionStorage.level
    };
  },
  created() {},
  methods: {
    doSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.dialog = true;
          if(!this.form.dept.id){
             this.$message({
              message: '所属部门不能为空',
              type: 'warning'
            })
            return
          }
          if (this.is_edit) {
            _jobUpdate(
              this.form,
              res => {
                 this.$message({
                  message: "操作成功",
                  type: "success"
                });
                this.dialog = false;
                this.cur_this.getList();
                this.dataInit();
              },
              err => {}
            );
          } else {
            _jobCreate(
              this.form,
              res => {
                this.$message({
                  message: "操作成功",
                  type: "success"
                });
                this.dialog = false;
                this.cur_this.getList();
                this.dataInit();
              },
              err => {}
            );
          }
        }
      });
    },
    domInit() {
     
    },
    dataInit() {
      this.form = {
        name: "",
        sort: 1,
        enabled: true,
        dept: {
          id: 0
        }
      };
    },
    closeDialog() { this.$refs["form"].resetFields();}
  }
};
</script>

<style lang="scss">
.el-input-number .el-input__inner {
  text-align: left;
}
</style>
